package com.zx.flow.core.execute;

import com.zx.flow.api.dict.FlowDict.Event;
import com.zx.flow.api.execute.model.FlowContext;
import com.zx.flow.api.execute.model.FlowNode;
import com.zx.flow.api.util.FlowDataUtil;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
 * 事件-准入-校验器
 *
 * @author zhangxue
 */
public abstract class EventInChecker {

    /**
     * 判断不为空
     *
     * @param obj 可以判断， null,string,Collection,Map,
     * @return
     */
    public static boolean notEmpty(Object obj) {
        if (obj == null) {
            return false;
        } else if (obj instanceof String) {
            return !StringUtils.isEmpty(obj);
        } else if (obj instanceof Collection) {
            return !CollectionUtils.isEmpty((Collection) obj);
        } else if (obj instanceof Map) {
            return !CollectionUtils.isEmpty((Map) obj);
        }
        return true;
    }

    /**
     * 判断nodeId是否为当前节点
     *
     * @param flow
     * @param nodeId
     * @return
     */
    public static boolean currentNode(FlowContext flow, String nodeId) {
        FlowNode current = FlowDataUtil.getCurrent(flow);
        return current == null ? false : !Objects.equals(current.getId(), nodeId);
    }

    /**
     * 判断事件
     *
     * @param event
     * @param checkList
     */
    public static boolean event(Event event, Event... checkList) {
        for (Event check : checkList) {
            if (Objects.equals(event, check)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断操作人是否有权限操作[不应该在in中判断，应该在on的时候判断]
     */
    public static boolean operator(FlowContext flow, String accountCode) {
        return false;
    }
}
